package com.i1510.util;


import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import cn.hutool.jwt.JWT;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

@Component
@Configuration
public class SignUtils {


    static String  hsyTokenKey;

    static String hsyJwt;

    static String hsyMerId;

    static String hsyUrl;

    static String hsyApiOrderGetOrderListByPay;

    static String hsyHmacMD5;

    @Value("${hsy.tokenKey}")
    public void setHsyTokenKey(String hsyTokenKey) {
        SignUtils.hsyTokenKey = hsyTokenKey;
    }

    @Value("${hsy.jwt}")
    public void setHsyJwt(String hsyJwt) {
        SignUtils.hsyJwt = hsyJwt;
    }

    @Value("${hsy.merId}")
    public void setHsyMerId(String hsyMerId) {
        SignUtils.hsyMerId = hsyMerId;
    }

    @Value("${hsy.url}")
    public void setHsyUrl(String hsyUrl) {
        SignUtils.hsyUrl = hsyUrl;
    }

    @Value("${hsy.api.order.getOrderListByPay}")
    public void setHsyApiOrderGetOrderListByPay(String hsyApiOrderGetOrderListByPay) {
        SignUtils.hsyApiOrderGetOrderListByPay = hsyApiOrderGetOrderListByPay;
    }

    @Value("${hsy.hmacMD5}")
    public void setHsyHmacMD5(String hsyHmacMD5) {
        SignUtils.hsyHmacMD5 = hsyHmacMD5;
    }

    public static String tokenValue(){
        String rtn = "";

        String tokenValue = JWT.create()
                .setPayload("merId", hsyMerId) //业务系统分配的商户号
                .setPayload("key", hsyTokenKey) //业务系统的加密密钥
                .setPayload("url", hsyUrl) // 业务系统地址
                .setKey(hsyJwt.getBytes())  //JWT密钥
                .sign();
        System.out.println(tokenValue);
        rtn = tokenValue;
        return rtn;
    }

    public static String sign(SortedMap<String, Object> parmas,Long curTime){
        String rtn = "";

        StringBuffer strA = new StringBuffer();
        parmas.forEach((key, value) -> {
            strA.append("&" + key + "=" + value);
        });
        System.out.println("strA=" + strA.toString());

        String strB = strA.toString().replaceFirst("&", "") + "#time=" + curTime;
        System.out.println("strB=" + strB);


        byte[] key = hsyHmacMD5.getBytes();
        HMac mac = new HMac(HmacAlgorithm.HmacMD5, key);
        String macHex = mac.digestHex(strB);
        System.out.println("strB加密后："+macHex);
        rtn = macHex;
        return rtn;
    }
    public static void main(String[] args) throws Exception {
        String tokenValue = JWT.create()
                .setPayload("merId", "5107") //业务系统分配的商户号
                .setPayload("key", "8f1f56eeab874ab4ad2c4133dadc9f26") //业务系统的加密密钥
                .setPayload("url", "https://shop5107.heleasy.cn/helapi") // 业务系统地址
                .setKey("Px2MMS1Q2Y5gKeqE9DEAi8Q273CRuOZP".getBytes())  //JWT密钥
                .sign();
        System.out.println(tokenValue);

        SortedMap<String, Object> parmas = new TreeMap<>();
        parmas.put("merId", 5107);
        parmas.put("startTime", "2022-11-26 00:00:00");
        parmas.put("endTime", "2022-11-26 00:00:00");


        System.out.println(parmas);

        long beginTime = System.currentTimeMillis() / 1000;
        System.out.println(beginTime);
        System.exit(0);

        StringBuffer strA = new StringBuffer();
        parmas.forEach((key, value) -> {
            strA.append("&" + key + "=" + value);
        });
        System.out.println("strA=" + strA.toString());

        String strB = strA.toString().replaceFirst("&", "") + "#time=" + beginTime;
        System.out.println("strB=" + strB);


        byte[] key = "MEmgMAid12x25n1APx2iV2ChNgg3RutZ".getBytes();
        HMac mac = new HMac(HmacAlgorithm.HmacMD5, key);
        String macHex = mac.digestHex(strB);
        System.out.println("strB加密后："+macHex);


        Map<String, String> params = new HashMap<>();
        params.put("merId","5107");
        params.put("startTime","2022-10-01 00:00:00");
        params.put("endTime","2022-10-31 00:00:00");
        System.out.println(JSONObject.toJSON(params));


    }
}
